package com.renderer
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import mx.containers.Box;
	import mx.controls.PopUpMenuButton;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.core.ScrollPolicy;
	import mx.events.DropdownEvent;
	import mx.events.FlexMouseEvent;
	import mx.events.MenuEvent;
	import mx.managers.PopUpManager;
	
	[Event (name="clickHeaderButton", type="flash.events.Event")]
	
	[Event (name="buttonItemClick", type="flash.events.Event")]
	
	public class ColumnNumeracion extends DataGridColumn
	{
		[Bindable]
		[Embed(source="assets/icoNada.png")]
		public var icoNada:Class;

		private var pumbMenu:PopUpMenuButton = new PopUpMenuButton();
		private var boxPop:Box = new Box;
		private var abierto:Boolean = false;
		private var cerrar:Boolean = false;
		private var mostrandoPop:Boolean = false;
		
		private var sincro:Timer = new Timer(100,1);
		private var _itemSeleccionado:Object = null;
		
		public var viewButton:Boolean = true;
		public var viewHeaderButton:Boolean = true;
		
		private var hizoClick:Boolean = false;
		private var timerBtn:Timer;
		private var eventoBtn:MenuEvent;
		private var dispatchButtonClick:Boolean = false;
		
		private var ultimoPunto:Point = null;
		private var altoFila:int = 0;
		
		public function ColumnNumeracion(columnName:String=null){
			super(columnName);
			
			pumbMenu.label = "OPCIONES";
			pumbMenu.styleName = "ComboBox";
			pumbMenu.setStyle("fontSize", "9");
			pumbMenu.useHandCursor = true;
			pumbMenu.buttonMode = true;
			pumbMenu.height = 18;
			pumbMenu.width = 100;
			
			//pumbMenu.setStyle("icon", icoNada);
			pumbMenu.openAlways = true;
			pumbMenu.addEventListener(MenuEvent.ITEM_CLICK, pumb_itemClick);
			pumbMenu.addEventListener(MouseEvent.CLICK , pumb_Click);
			pumbMenu.addEventListener(DropdownEvent.OPEN, openPop);
			pumbMenu.addEventListener(DropdownEvent.CLOSE, closePop);
			boxPop.setStyle("horizontalAlign", "center");
			boxPop.setStyle("verticalAlign", "middle");
			boxPop.setStyle("paddingRight", "0");
			boxPop.setStyle("paddingLeft", "0");
			boxPop.setStyle("paddingTop", "0");
			boxPop.setStyle("paddingBottom", "0");
			boxPop.height = 24;
			boxPop.horizontalScrollPolicy = ScrollPolicy.OFF;
			boxPop.verticalScrollPolicy = ScrollPolicy.OFF;
			boxPop.addChild(pumbMenu);
			boxPop.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, removeBoton);
			boxPop.addEventListener(FlexMouseEvent.MOUSE_WHEEL_OUTSIDE, removeBoton);
			
			sincro.addEventListener(TimerEvent.TIMER_COMPLETE, sincroComplete);
		}
		
		public function set menuProvider(value:Array):void{
			pumbMenu.dataProvider = value;
		}
		
		private function removeBoton(event:FlexMouseEvent):void{
			//Alert.show("alto: " + altoFila + "  M: " + event.localX.toString() + " " + event.localY.toString() + "  B: " + ultimoPunto.x.toString() + " " + ultimoPunto.y.toString());
			if(event!=null){
				if(event.type == FlexMouseEvent.MOUSE_DOWN_OUTSIDE){
					if(event.localY >= 0 && event.localY <= (altoFila - 1)){
						return;
					}
				}
			}
			quitarBoton();
		}
		
		public function quitarBoton(event:Event = null):void{
			if(abierto){
				cerrar = true;
				closePop(null);
			}else{
				PopUpManager.removePopUp(boxPop);
				mostrandoPop = false;
				if(dispatchButtonClick){
					dispatchEvent(new Event("buttonItemClick"));
					dispatchButtonClick = false;
				}
			}
		}
		
		
		public function openPop(event:Event):void{
			abierto = true;
			//pumbMenu.popUp.width = 120;
		}
		
		private function closePop(event:Event):void{
			if(sincro.running){
				sincro.stop();
			}
			
			if(cerrar){
				sincro.delay = pumbMenu.getStyle("closeDuration");
				sincro.reset();
				sincro.start();
			}
		}
		
		private function sincroComplete(event:TimerEvent):void{
			abierto = false;
			cerrar = false;
			quitarBoton();
		}
		
		
		public function ponerBoton(point:Point, alto:int):void{
			var alineacion:Number = 0.0;
			
			altoFila = alto;
			ultimoPunto = point;
			
			if(alto < (pumbMenu.height + 4)){
				alineacion = (pumbMenu.height + 4 - alto)/2.0;
				alto = pumbMenu.height + 4;
			}
			
			boxPop.height = alto;
			
			if(!mostrandoPop){
				mostrandoPop = true;
				PopUpManager.addPopUp(boxPop, FlexGlobals.topLevelApplication.boxPrincipal,false);
			}
			
			pumbMenu.setStyle("icon", icoNada);
			boxPop.x = point.x;
			boxPop.y = point.y - alineacion - 2;
			
			//Alert.show("X: " + point.x.toString() + " Y:" + point.y.toString() + " " + alineacion.toString() + " " + alto.toString() + " Y:" + boxPop.y.toString()) 
		}
		
		public function get itemSeleccionado():Object{
			return _itemSeleccionado;
		}
		
		
		private function pumb_itemClick(event:MenuEvent):void{
			eventoBtn = event;
			
			timerBtn = new Timer(30, 1);
			timerBtn.addEventListener(TimerEvent.TIMER_COMPLETE, timerBtn_timerComplete);
			timerBtn.start();
		}
		
		private function pumb_Click(evt:Event):void{
			hizoClick = true;
		}
		
		private function timerBtn_timerComplete(evt:TimerEvent):void {
			if(!hizoClick){
				_itemSeleccionado = eventoBtn.item;
				dispatchButtonClick = true;
				quitarBoton();
			}
			
			hizoClick = false;
			timerBtn.reset();
		}
	}
}